package org.admanager.piadl.plugin.diagram.part;

import org.admanager.piadl.plugin.diagram.edit.parts.AcmeRootEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ArchitectureArchitectureCompartmentEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ArchitectureEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ArchitectureNameEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.Component2EditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ComponentComponentCompartment2EditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ComponentComponentCompartmentEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ComponentEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ComponentName2EditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ComponentNameEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.Connector2EditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ConnectorConnectorCompartment2EditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ConnectorConnectorCompartmentEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ConnectorEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ConnectorName2EditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.ConnectorNameEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.Port2EditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.PortEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.RelayEditPart;
import org.admanager.piadl.plugin.diagram.edit.parts.UnificationEditPart;
import org.admanager.piadladmanagement.plugin.piadladmanagement.Architecture;
import org.admanager.piadladmanagement.plugin.piadladmanagement.PiadladmanagementPackage;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;

/**
 * This registry is used to determine which type of visual object should be
 * created for the corresponding Diagram, Node, ChildNode or Link represented
 * by a domain model object.
 * 
 * @generated
 */
public class PiADLADManagementMetamodelVisualIDRegistry {

	/**
	 * @generated
	 */
	private static final String DEBUG_KEY = "org.admanager.piadl.plugin.diagram/debug/visualID"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static int getVisualID(View view) {
		if (view instanceof Diagram) {
			if (AcmeRootEditPart.MODEL_ID.equals(view.getType())) {
				return AcmeRootEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		return org.admanager.piadl.plugin.diagram.part.PiADLADManagementMetamodelVisualIDRegistry
				.getVisualID(view.getType());
	}

	/**
	 * @generated
	 */
	public static String getModelID(View view) {
		View diagram = view.getDiagram();
		while (view != diagram) {
			EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
			if (annotation != null) {
				return (String) annotation.getDetails().get("modelID"); //$NON-NLS-1$
			}
			view = (View) view.eContainer();
		}
		return diagram != null ? diagram.getType() : null;
	}

	/**
	 * @generated
	 */
	public static int getVisualID(String type) {
		try {
			return Integer.parseInt(type);
		} catch (NumberFormatException e) {
			if (Boolean.TRUE.toString().equalsIgnoreCase(
					Platform.getDebugOption(DEBUG_KEY))) {
				PiADLADManagementMetamodelDiagramEditorPlugin.getInstance()
						.logError(
								"Unable to parse view type as a visualID number: "
										+ type);
			}
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static String getType(int visualID) {
		return Integer.toString(visualID);
	}

	/**
	 * @generated
	 */
	public static int getDiagramVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (PiadladmanagementPackage.eINSTANCE.getArchitecture().isSuperTypeOf(
				domainElement.eClass())
				&& isDiagram((Architecture) domainElement)) {
			return AcmeRootEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static int getNodeVisualID(View containerView, EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		String containerModelID = org.admanager.piadl.plugin.diagram.part.PiADLADManagementMetamodelVisualIDRegistry
				.getModelID(containerView);
		if (!AcmeRootEditPart.MODEL_ID.equals(containerModelID)) {
			return -1;
		}
		int containerVisualID;
		if (AcmeRootEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = org.admanager.piadl.plugin.diagram.part.PiADLADManagementMetamodelVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = AcmeRootEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		switch (containerVisualID) {
		case AcmeRootEditPart.VISUAL_ID:
			if (PiadladmanagementPackage.eINSTANCE.getConnector()
					.isSuperTypeOf(domainElement.eClass())) {
				return ConnectorEditPart.VISUAL_ID;
			}
			if (PiadladmanagementPackage.eINSTANCE.getComponent()
					.isSuperTypeOf(domainElement.eClass())) {
				return ComponentEditPart.VISUAL_ID;
			}
			break;
		case ConnectorConnectorCompartmentEditPart.VISUAL_ID:
			if (PiadladmanagementPackage.eINSTANCE.getArchitecture()
					.isSuperTypeOf(domainElement.eClass())) {
				return ArchitectureEditPart.VISUAL_ID;
			}
			if (PiadladmanagementPackage.eINSTANCE.getPort().isSuperTypeOf(
					domainElement.eClass())) {
				return PortEditPart.VISUAL_ID;
			}
			break;
		case ArchitectureArchitectureCompartmentEditPart.VISUAL_ID:
			if (PiadladmanagementPackage.eINSTANCE.getComponent()
					.isSuperTypeOf(domainElement.eClass())) {
				return Component2EditPart.VISUAL_ID;
			}
			if (PiadladmanagementPackage.eINSTANCE.getConnector()
					.isSuperTypeOf(domainElement.eClass())) {
				return Connector2EditPart.VISUAL_ID;
			}
			break;
		case ComponentComponentCompartmentEditPart.VISUAL_ID:
			if (PiadladmanagementPackage.eINSTANCE.getPort().isSuperTypeOf(
					domainElement.eClass())) {
				return Port2EditPart.VISUAL_ID;
			}
			if (PiadladmanagementPackage.eINSTANCE.getArchitecture()
					.isSuperTypeOf(domainElement.eClass())) {
				return ArchitectureEditPart.VISUAL_ID;
			}
			break;
		case ConnectorConnectorCompartment2EditPart.VISUAL_ID:
			if (PiadladmanagementPackage.eINSTANCE.getArchitecture()
					.isSuperTypeOf(domainElement.eClass())) {
				return ArchitectureEditPart.VISUAL_ID;
			}
			if (PiadladmanagementPackage.eINSTANCE.getPort().isSuperTypeOf(
					domainElement.eClass())) {
				return PortEditPart.VISUAL_ID;
			}
			break;
		case ComponentComponentCompartment2EditPart.VISUAL_ID:
			if (PiadladmanagementPackage.eINSTANCE.getPort().isSuperTypeOf(
					domainElement.eClass())) {
				return Port2EditPart.VISUAL_ID;
			}
			if (PiadladmanagementPackage.eINSTANCE.getArchitecture()
					.isSuperTypeOf(domainElement.eClass())) {
				return ArchitectureEditPart.VISUAL_ID;
			}
			break;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static boolean canCreateNode(View containerView, int nodeVisualID) {
		String containerModelID = org.admanager.piadl.plugin.diagram.part.PiADLADManagementMetamodelVisualIDRegistry
				.getModelID(containerView);
		if (!AcmeRootEditPart.MODEL_ID.equals(containerModelID)) {
			return false;
		}
		int containerVisualID;
		if (AcmeRootEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = org.admanager.piadl.plugin.diagram.part.PiADLADManagementMetamodelVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = AcmeRootEditPart.VISUAL_ID;
			} else {
				return false;
			}
		}
		switch (containerVisualID) {
		case AcmeRootEditPart.VISUAL_ID:
			if (ConnectorEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ComponentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ConnectorEditPart.VISUAL_ID:
			if (ConnectorNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ConnectorConnectorCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ComponentEditPart.VISUAL_ID:
			if (ComponentNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ComponentComponentCompartment2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ArchitectureEditPart.VISUAL_ID:
			if (ArchitectureNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ArchitectureArchitectureCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case Component2EditPart.VISUAL_ID:
			if (ComponentName2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ComponentComponentCompartmentEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case Connector2EditPart.VISUAL_ID:
			if (ConnectorName2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ConnectorConnectorCompartment2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ConnectorConnectorCompartmentEditPart.VISUAL_ID:
			if (ArchitectureEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (PortEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ArchitectureArchitectureCompartmentEditPart.VISUAL_ID:
			if (Component2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (Connector2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ComponentComponentCompartmentEditPart.VISUAL_ID:
			if (Port2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ArchitectureEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ConnectorConnectorCompartment2EditPart.VISUAL_ID:
			if (ArchitectureEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (PortEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ComponentComponentCompartment2EditPart.VISUAL_ID:
			if (Port2EditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ArchitectureEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static int getLinkWithClassVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (PiadladmanagementPackage.eINSTANCE.getUnification().isSuperTypeOf(
				domainElement.eClass())) {
			return UnificationEditPart.VISUAL_ID;
		}
		if (PiadladmanagementPackage.eINSTANCE.getRelay().isSuperTypeOf(
				domainElement.eClass())) {
			return RelayEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * User can change implementation of this method to handle some specific
	 * situations not covered by default logic.
	 * 
	 * @generated
	 */
	private static boolean isDiagram(Architecture element) {
		return true;
	}

}
